package com.spn.blog.api.service.impl;

import com.spn.blog.common.dao.pojo.LoginUser;
import com.spn.blog.common.dao.pojo.SysUser;
import com.spn.blog.service.login.SysUserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @DubboReference
    private SysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 去从数据库查询用户是否存在
        SysUser sysUser = sysUserService.findUserByAccount(username);
        if(sysUser == null) {
            throw new RuntimeException("用户名错误");
        }
        // 鉴权
        List<String> authorities = sysUserService.selectPermsByUserId(sysUser.getId());
        // 返回 UserDetail 对象
        return new LoginUser(sysUser,authorities);
    }
}